package com.atguigu.tingshu.order.strategy.impl;
import java.math.BigDecimal;
import java.util.Arrays;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.client.VipServiceConfigFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;


import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class VipOrderTradeStrategy implements OrderTradeStrategy {


    @Autowired
    private RedisTemplate<String , String> redisTemplate ;
    @Autowired
    private  VipServiceConfigFeignClient vipServiceConfigFeignClient;
    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {
        log.info("VipOrderTradeStrategy...trade方法执行了....");
        /*OrderInfoVo orderInfoVo = new OrderInfoVo();
        Result<VipServiceConfig> vipServiceConfigResult = vipServiceConfigFeignClient.findById(tradeVo.getItemId());
        VipServiceConfig vipServiceConfig = vipServiceConfigResult.getData();
        String tradeNo = UUID.randomUUID().toString().replaceAll("-","");
        orderInfoVo.setTradeNo(tradeNo);
        orderInfoVo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
        orderInfoVo.setItemType(tradeVo.getItemType());


        orderInfoVo.setOriginalAmount(vipServiceConfig.getPrice());
        //优惠金额
        orderInfoVo.setDerateAmount(vipServiceConfig.getPrice().subtract(vipServiceConfig.getDiscountPrice()));
        //订单总价 也就是折后价
        orderInfoVo.setOrderAmount(vipServiceConfig.getDiscountPrice());

        //获取vip的统计数据
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(tradeVo.getItemId());
        orderDetailVo.setItemName(vipServiceConfig.getName());
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVo.setItemPrice(vipServiceConfig.getDiscountPrice());
        orderInfoVo.setOrderDetailVoList(Arrays.asList(orderDetailVo));


        //构建减免明细数据
        BigDecimal derateAmount = orderInfoVo.getDerateAmount();
        if(0 != derateAmount.intValue()){
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(orderInfoVo.getDerateAmount());
            orderDerateVo.setRemarks("vip见面明细");
            orderInfoVo.setOrderDerateVoList(Arrays.asList(orderDerateVo));
        }

        orderInfoVo.setTimestamp(SignHelper.getTimestamp());  //当前系统的时间戳
        *//*
        * 数据签名 就是从orderInfoVo对象中获取相关字段的数据 然后对数据进行加密,得到签名
        * 作用:后期进行订单提交的时候,需要使用到该数据提交的合法性
        * *//*
        Map<String,Object> map = JSON.parseObject(JSON.toJSONString(orderInfoVo),Map.class);
        String sign = SignHelper.getSign(map);
        orderInfoVo.setSign(sign);*/
        //具体是返回一个orderInfoVo返回给前端\
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        Result<VipServiceConfig> vipServiceConfigFeignClientById = vipServiceConfigFeignClient.findById(tradeVo.getItemId());
        VipServiceConfig vipServiceConfig = vipServiceConfigFeignClientById.getData();
        //统计订单明细数据
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(tradeVo.getItemId());
        orderDetailVo.setItemName(vipServiceConfig.getName());
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVo.setItemPrice(vipServiceConfig.getPrice());
        //根据id查询订单明细数据
        orderInfoVo.setOriginalAmount(vipServiceConfig.getPrice());
        orderInfoVo.setDerateAmount(vipServiceConfig.getPrice().subtract(vipServiceConfig.getDiscountPrice()));
        orderInfoVo.setOrderAmount(vipServiceConfig.getDiscountPrice());
        orderInfoVo.setOrderDetailVoList(Arrays.asList(orderDetailVo));

        //构建减免数据
        //首先判断 是否有减免
        BigDecimal derateAmount = orderInfoVo.getDerateAmount();
        if(0!=derateAmount.intValue()){
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            //减免类型
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("vip见面标记");
            orderInfoVo.setOrderDerateVoList(Arrays.asList(orderDerateVo));
        }

        this.setOtherInfo(orderInfoVo,tradeVo,redisTemplate);

        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_VIP;
    }
}
